<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Proxy.revocable()</title>
</head>
<body>
    <h1>Proxy.revocable()</h1>
    <h2>返回一个可取消的Proxy实例</h2>

    <script>
        const target = {};
        const handler = {};

        const {proxy, revoke} = Proxy.revocable(target, handler);

        proxy.foo = 123;
        console.log(proxy.foo); // 123

        revoke();
        // console.log(proxy.foo); // TypeError: Cannot perform 'get' on a proxy that has been revoked

        // 使用场景：目标对象不允许直接访问，必须通过代理访问，一旦访问结束，就收回代理权
        function createMyAPI() {
            const target = {};  // API
            const handler = {}; // 处理器
            const {proxy, revoke} = Proxy.revocable(target, handler);
            
            // 设置定时器，10秒后收回代理权
            setTimeout(() => {
                revoke();
            }, 10000);
            
            return proxy;
        }

        const api = createMyAPI();
    </script>
</body>
</html> 